# -*- coding:utf-8 -*-
"""
对磁盘进行操作

"""
import os


def B2K(_bytes):
    """
    字节数转KB
    :param _bytes:
    :return:
    """
    return int(_bytes) / 1024

def B2M(_bytes):
    """
    字节大小转M
    :param _bytes:
    :return:
    """
    return int(_bytes) / 1024 / 1024

def B2G(_bytes):
    """
    字节大小转G
    :param _bytes:
    :return:
    """
    return int(_bytes) / 1024 / 1024 / 1024

def B2T(_bytes):
    """
    字节大小转T
    :param _bytes:
    :return:
    """
    return int(_bytes) / 1024 / 1024 / 1024 / 1024




def getDiskFreeSpace(disk_name,bytes_type='B'):
    """
    查询指定磁盘的剩余空间
    :param disk_name:
    :param bytes_type: 'B' 返回原字节大小数,；'K'返回KB大小;'M'返回M大小;'T';返回T大小.
    :return:
    """

    cmd = '''wmic LogicalDisk where "Caption='{}:'" get FreeSpace /value'''.format(disk_name)

    result = os.popen(cmd)
    text = result.read()
    try:
        size = text.strip('\n').split('=')[1]
        if bytes_type == 'B':
            return int(size)
        elif bytes_type == 'K':
            return B2K(size)
        elif bytes_type == 'M':
            return B2M(size)
        elif bytes_type == 'G':
            return B2G(size)
        elif bytes_type == 'T':
            return B2T(size)

    except Exception as getDiskFreeSpace_ERR:
        print('None',getDiskFreeSpace_ERR)
        return None


